package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UserCoverResp
import com.polaris.live.resp.go_resp.NullBean
import com.polaris.live.resp.go_resp.SettingModifyBean
import com.polaris.live.resp.go_resp.UpLoadImageBean

/**
 * SetLiveImageViewModel
 *
 * @author Created by 半仙 on 2023/11/4/004 11:29
 */
open class SetLiveImageViewModel : BaseViewModel() {

    //上传封面照
    val mUploadLiveData = MutableLiveData<ResultState<UserCoverResp>>()
    fun upLoadImage(url: String) {
        requestLoading({ userService.upLoadImage(UpLoadImageBean(url)) }, mUploadLiveData)
    }

    //获取用户背景照
    val mUserCoverLiveData = MutableLiveData<ResultState<UserCoverResp>>()
    fun userCover() {
        request({ userService.coverPhotos() }, mUserCoverLiveData)
    }

    //提交背景照
    val mSubmitLiveData = MutableLiveData<ResultState<UserCoverResp>>()
    fun submitPhoto() {
        requestLoading({ userService.submitPhotos(NullBean()) }, mSubmitLiveData)
    }

    //上传背景预检查
    val coverUploadLiveData = MutableLiveData<ResultState<Any>>()
    fun coverPhotosUpload() {
        request({ userService.coverPhotosUpload() }, coverUploadLiveData)
    }

    /**
     * 修改用户设置
     */
    val settingLiveData = MutableLiveData<Int>()
    fun settingModify(@UserConst.SettingType type: Int, state: Int) {
        requestLoading({ userService.settingModify(SettingModifyBean(type, state)) }, {
            when (type) {
                UserConst.UserSetting.LIVE_MODULE_SWITFCH -> {
                    UserManager.getUser().setting?.liveSwitch = state
                }
            }
            settingLiveData.value = AppConst.YES
        }, {
            ToastGravity.showGravity(it.errorMsg)
        })
    }
}